package com.android.camara;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;


public class CamaraDemo extends Activity implements OnClickListener {

	NativeLib nativeLib;

	private static final String TAG = "CamaraDemo";
	Camera camera;
	Preview preview;
	Button buttonClick;
	Button seeImage;

	Button grises;
	Button toNegative;
	Button nuevaCaptura;
	Button salir;

	public Bitmap mapa = null;
	private ImageView imagenVista = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		nativeLib = new NativeLib();

		preview = new Preview(this);
		((FrameLayout) findViewById(R.id.preview)).addView(preview);

		buttonClick = (Button) findViewById(R.id.buttonClick);
		seeImage = (Button) findViewById(R.id.buttonSeeImage);

		buttonClick.setOnClickListener(this);
		seeImage.setOnClickListener(this);

	}

	ShutterCallback shutterCallback = new ShutterCallback() {
		public void onShutter() {
			Log.d(TAG, "onShutter'd");
		}
	};

	/** Handles data for raw picture */
	PictureCallback rawCallback = new PictureCallback() {
		public void onPictureTaken(byte[] data, Camera camera) {
			Log.d(TAG, "onPictureTaken - raw");

		}
	};

	/** Handles data for jpeg picture */
	PictureCallback jpegCallback = new PictureCallback() {
		public void onPictureTaken(byte[] data, Camera camera) {
			mapa = BitmapFactory.decodeByteArray(data, 0, data.length);
			mapa = Bitmap.createScaledBitmap(mapa, 200, 250, true);
		//	int res;
		//	int M[][]=new int[3][3];
			FileOutputStream outStream = null;
			try {
				
				//R=cvGetSize(M);
				outStream = new FileOutputStream(String.format("/sdcard/%s.jpg","hola"));	
				outStream.write(data);
				outStream.close();
				Log.d(TAG, "onPreviewFrame - wrote bytes: " + data.length);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
			}
				
			
		}
	};

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		// Tomar la fotografia
		case R.id.buttonClick:
			preview.camera.takePicture(shutterCallback, rawCallback,
					jpegCallback);
			break;

		case R.id.buttonInverse:
			int[] graypixls = new int[mapa.getWidth() * mapa.getHeight()];

			mapa.getPixels(graypixls, 0, mapa.getWidth(), 0, 0,
					mapa.getWidth(), mapa.getHeight());
			mapa.setPixels(nativeLib.convertToNegative(graypixls), 0,
					mapa.getWidth(), 0, 0, mapa.getWidth(), mapa.getHeight());

			imagenVista.setImageBitmap(mapa);
			break;

		case R.id.buttongray:
			
			int width = mapa.getWidth();
			int height = mapa.getHeight();
			int size = width * height;

			int[] pixeles = new int[size];

			// obtener los pixeles
			mapa.getPixels(pixeles, 0, mapa.getWidth(), 0, 0, width, height);
			mapa.setPixels(nativeLib.convertToGray(pixeles), 0,
					mapa.getWidth(), 0, 0, width, height);
			
			
			imagenVista.setImageBitmap(mapa);
			break;
			
		case R.id.buttonSalir:
			finish();
			break;

		case R.id.buttonSeeImage:
			setContentView(R.layout.picture);

			imagenVista = (ImageView) findViewById(R.id.imageView1);

			grises=(Button)findViewById(R.id.buttongray);
			toNegative = (Button) findViewById(R.id.buttonInverse);
			salir = (Button) findViewById(R.id.buttonSalir);
			nuevaCaptura = (Button) findViewById(R.id.buttonNuevaFoto);

			grises.setOnClickListener(this);
			toNegative.setOnClickListener(this);
			salir.setOnClickListener(this);
			nuevaCaptura.setOnClickListener(this);

			imagenVista.setImageBitmap(mapa);

			break;
		case R.id.buttonNuevaFoto:
			setContentView(R.layout.main);

			buttonClick = (Button) findViewById(R.id.buttonClick);
			seeImage = (Button) findViewById(R.id.buttonSeeImage);

			buttonClick.setOnClickListener(this);
			seeImage.setOnClickListener(this);

			preview = new Preview(this);
			((FrameLayout) findViewById(R.id.preview)).addView(preview);
			

			break;

		default:
			break;
		}
	}
}